home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Plug-in - WireFrame Renderer / SR.h < prev   
Encoding:
C/C++ Source or Header  |  1997-08-14  |  13.7 KB  |  529 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        SR.h                                                     **
  4.  **                                                                          **
  5.  **                                                                          **
  6.  **     Purpose:     Sample renderer  header                                       **
  7.  **                                                                          **
  8.  **                                                                          **
  9.  **                                                                          **
  10.  **     Copyright (C) 1996 Apple Computer, Inc.  All rights reserved.          **
  11.  **                                                                          **
  12.  **                                                                          **
  13.  *****************************************************************************/
  14. #ifndef SR_h
  15. #define SR_h
  16.  
  17. #include <assert.h>
  18.  
  19. #include "QD3D.h"
  20. #include "QD3DCamera.h"
  21. #include "QD3DStyle.h"
  22. #include "QD3DRenderer.h"
  23.  
  24. #if defined(WINDOW_SYSTEM_MACINTOSH) && WINDOW_SYSTEM_MACINTOSH
  25.  
  26. #include <Quickdraw.h>
  27.  
  28. #elif defined (WINDOW_SYSTEM_WIN32) && WINDOW_SYSTEM_WIN32
  29.  
  30. #include <WINDOWS.h>
  31.  
  32. #endif
  33.  
  34. #include "SR_Math.h"
  35. #include "SR_Marker.h"
  36.  
  37.  
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif    /* __cplusplus */
  41.  
  42. /******************************************************************************
  43.  **                                                                             **
  44.  **                                Macros                                          **
  45.  **                                                                             **
  46.  *****************************************************************************/
  47.  
  48. /*
  49.  *  Keep the compiler from complaining about unused parameters.
  50.  */
  51. #define UNUSED(x)    x
  52.  
  53.  
  54. /*
  55.  *  For line pipeline, we may be in "polygon" or "polyline" mode. In the
  56.  *  former, we must connect the last vertex with the first; in the latter mode,
  57.  *  we must *not*.
  58.  */
  59. #define    DO_POLYLINE    0
  60. #define DO_POLYGON    1
  61.  
  62.  
  63. /*
  64.  * Fast math macros
  65.  */
  66. #define FLOAT_TRUNC_TO_LONG(f)                (long)(f)
  67. #define FLOAT_ROUND_TO_LONG(f)     \
  68.     (((f) >= 0.0) ? (long)((f) + 0.5) : (long)((f) - 0.5))
  69.     
  70. #define FLOAT_ROUND_TO_LONG_POSITIVE(f)        (long)((f) + 0.5)
  71. #define FABS(f)                             (((f) >= 0.0F) ? (f) : -(f))
  72. #define ABS(x)                                 (((x) >= 0)    ? (x) : -(x))
  73.  
  74.  
  75.  /*
  76.  *  Pixel-packing macros. 32-bit version assumes Macintosh ordering of
  77.  *  r, g, and b. Other platforms may have these in a different order, in which
  78.  *  case this macro should have the packing reordered. Other bit depths may
  79.  *  be accomodated by adding similar macros, with the appropriate shifts replacing
  80.  *  those found in this macro. Ordering is same on Win95.
  81.  */
  82. #define    PACK_RGB_32(_r, _g, _b) (                                                 \
  83.     ((unsigned long) 0xFF000000) |                                                 \
  84.     ((unsigned long)(FLOAT_ROUND_TO_LONG_POSITIVE((_r) * 255.0) & 0xFF) << 16) |\
  85.     ((unsigned long)(FLOAT_ROUND_TO_LONG_POSITIVE((_g) * 255.0) & 0xFF) << 8)  |\
  86.     ((unsigned long)(FLOAT_ROUND_TO_LONG_POSITIVE((_b) * 255.0) & 0xFF)))
  87.     
  88. #if defined(WINDOW_SYSTEM_MACINTOSH) && WINDOW_SYSTEM_MACINTOSH
  89.  
  90. /*
  91.  *  Convert a (0 - 1) valued RGB specification to a pixel. For 32-
  92.  *  bit pixel, pack in the RGB values; for 8-bit pixels, call the 
  93.  *  QuickDraw function that converts a 16-bit color to a colormap
  94.  *  index. Other platforms may require a different function call
  95.  *  to map the RGB values to a pixel index.
  96.  */
  97. #define    COLOR_TO_PIXEL(_result, _drawRegion, _depth, _r, _g, _b)     \
  98.     switch (_depth) {                                                 \
  99.         case kQ3XDevicePixelTypeRGB32:                                 \
  100.         case kQ3XDevicePixelTypeARGB32:                             \
  101.             _result = PACK_RGB_32(_r, _g, _b);                        \
  102.             break;                                                     \
  103.         case kQ3XDevicePixelTypeIndexed8: {                            \
  104.             RGBColor    _rgbColor;                                    \
  105.                                                                     \
  106.             _rgbColor.red     = _r * 65535;                            \
  107.             _rgbColor.green = _g * 65535;                            \
  108.             _rgbColor.blue     = _b * 65535;                            \
  109.                                                                     \
  110.             _result = Color2Index(&_rgbColor);                        \
  111.             break;                                                     \
  112.         }                                                            \
  113.         default:                                                     \
  114.             assert(0);                                                \
  115.             break;                                                     \
  116.     }
  117.     
  118. #define    COLOR_8_TO_INDEX(_result, _drawRegion, _depth, _r, _g, _b)     \
  119.     switch (_depth) {                                                 \
  120.         case kQ3XDevicePixelTypeIndexed8: {                            \
  121.             RGBColor    _rgbColor;                                    \
  122.                                                                     \
  123.             _rgbColor.red     = _r << 8;                                \
  124.             _rgbColor.green = _g << 8;                                \
  125.             _rgbColor.blue     = _b << 8;                                \
  126.                                                                     \
  127.             _result = Color2Index(&_rgbColor);                        \
  128.             break;                                                     \
  129.         }                                                            \
  130.         default:                                                     \
  131.             assert(0);                                                \
  132.             break;                                                     \
  133.     }
  134.  
  135. #define    COLOR_5_TO_INDEX(_result, _drawRegion, _depth, _r, _g, _b)     \
  136.     switch (_depth) {                                                 \
  137.         case kQ3XDevicePixelTypeIndexed8: {                            \
  138.             RGBColor    _rgbColor;                                    \
  139.                                                                     \
  140.             _rgbColor.red     = _r << 11;                                \
  141.             _rgbColor.green = _g << 11;                                \
  142.             _rgbColor.blue     = _b << 11;                                \
  143.                                                                     \
  144.             _result = Color2Index(&_rgbColor);                        \
  145.             break;                                                     \
  146.         }                                                            \
  147.         default:                                                     \
  148.             assert(0);                                                \
  149.             break;                                                     \
  150.     }
  151.  
  152. #elif defined(WINDOW_SYSTEM_WIN32) && WINDOW_SYSTEM_WIN32
  153.  
  154. /*
  155.  *  Convert a (0 - 1) valued RGB specification to a pixel. For 32-
  156.  *  bit pixel, pack in the RGB values; All other formats are
  157.  *    not supported 
  158.  */
  159.  
  160. #define    COLOR_TO_PIXEL(_result, _drawRegion, _depth, _r, _g, _b)     \
  161.     switch (_depth) {                                                 \
  162.         case kQ3XDevicePixelTypeRGB32:                                 \
  163.         case kQ3XDevicePixelTypeARGB32:                             \
  164.             _result = PACK_RGB_32(_r, _g, _b);                        \
  165.             break;                                                     \
  166.         default:                                                     \
  167.             assert(0);                                                \
  168.             break;                                                     \
  169.     }
  170.  
  171. /*
  172.  *    This should never be called on Windows since we never have 
  173.  *    indexed Draw Contexts.
  174.  */
  175.  
  176. #define    COLOR_8_TO_INDEX(_result, _drawRegion, _depth, _r, _g, _b)     \
  177.     switch (_depth) {                                                 \
  178.         default:                                                     \
  179.             assert(0);                                                \
  180.             break;                                                     \
  181.     }
  182.  
  183. /*
  184.  *    This should never be called on Windows since we never have 
  185.  *    indexed Draw Contexts.
  186.  */
  187.  
  188. #define    COLOR_5_TO_INDEX(_result, _drawRegion, _depth, _r, _g, _b)     \
  189.     switch (_depth) {                                                 \
  190.         default:                                                     \
  191.             assert(0);                                                \
  192.             break;                                                     \
  193.     }
  194.  
  195. #endif
  196.  
  197. /******************************************************************************
  198.  **                                                                             **
  199.  **                                    Transforms                                 **
  200.  **                                                                             **
  201.  *****************************************************************************/
  202.  
  203. typedef struct TSRRenderTransforms {
  204.     TQ3Matrix4x4            localToWorld;
  205.     TQ3Boolean                dirtyLocalToWorld;
  206.     TQ3Boolean                validLocalToWorld;
  207.     
  208.     TQ3Matrix4x4            localToFrustum;
  209.     TQ3Boolean                dirtyLocalToFrustum;
  210.     TQ3Boolean                validLocalToFrustum;
  211.     
  212.     TQ3Matrix4x4            worldToFrustum;
  213.     TQ3Boolean                dirtyWorldToFrustum;
  214.     TQ3Boolean                validWorldToFrustum;
  215.     
  216.     TQ3Matrix4x4            frustumToDC;
  217.     TQ3Boolean                dirtyFrustumToDC;
  218.     TQ3Boolean                validFrustumToDC;
  219.     
  220.     TQ3Matrix4x4            localToDC;
  221.     TQ3Boolean                dirtyLocalToDC;
  222.     TQ3Boolean                validLocalToDC;
  223. } TSRRenderTransforms;
  224.  
  225.  
  226.  
  227. /******************************************************************************
  228.  **                                                                             **
  229.  **                                Clipping Macros                                 **
  230.  **                                                                             **
  231.  *****************************************************************************/
  232.  
  233. #define    SR_MIN_X        0x01
  234. #define    SR_MAX_X        0x02
  235. #define    SR_MIN_Y        0x04
  236. #define    SR_MAX_Y        0x08
  237. #define    SR_MIN_Z        0x10
  238. #define    SR_MAX_Z        0x20
  239. #define    SR_CLIP_ALL        0x3F
  240.  
  241. #define    SR_DRAW_NEXT    0xFF
  242.  
  243.  
  244. /******************************************************************************
  245.  **                                                                             **
  246.  **                                Data Structure                                 **
  247.  **                                                                             **
  248.  *****************************************************************************/
  249.                  
  250. typedef TQ3Status (*LineFunction2D)(
  251.     const struct TSRPrivate         *srPrivate, 
  252.     const TQ3Point3D                 *pt0,
  253.     const TQ3Point3D                 *pt1,
  254.     const TQ3ColorRGB                 *lineColor);
  255.  
  256. typedef TQ3Status (*PointFunction2D)(
  257.     const struct TSRPrivate         *srPrivate, 
  258.     const TQ3Point3D                 *pt0, 
  259.     const TQ3ColorRGB                 *pointColor);
  260.  
  261. typedef TQ3Status (*MarkerFunction2D)(
  262.     const struct TSRPrivate         *srPrivate, 
  263.     const TQ3Point3D                 *pt0, 
  264.     const TSRMarkerRasterData        *markerData,
  265.     const TQ3ColorRGB                 *pointColorRGB);
  266.  
  267. typedef TQ3Status (*PixmapMarkerFunction2D)(
  268.     const struct TSRPrivate         *srPrivate, 
  269.     const TQ3Point3D                 *pt0, 
  270.     const TSRPixmapMarkerRasterData    *markerData,
  271.     const TQ3ColorRGB                *highlightColor);
  272.  
  273. typedef struct TSRRasterFunctions {
  274.     LineFunction2D            lineFunction;
  275.     PointFunction2D            pointFunction;
  276.     MarkerFunction2D         markerFunction;
  277.     PixmapMarkerFunction2D    pixmapMarkerFunction;
  278. } TSRRasterFunctions;
  279.  
  280. enum {
  281.     SRcRasterBasic = 0,
  282.     SRcRasterClip  = 1
  283. };
  284.  
  285.  
  286. typedef struct TSRPrivate {
  287.     /*
  288.      *  Styles
  289.      */
  290.     TQ3BackfacingStyle                backfacingStyle;
  291.     TQ3OrientationStyle                orientationStyle;
  292.     TQ3FillStyle                    viewFillStyle;
  293.     
  294.     /*
  295.      *  Highlight attribute set and state (on or off)
  296.      */
  297.     TQ3AttributeSet                    viewHighlightAttributeSet;
  298.     TQ3Boolean                        viewHighlightState;
  299.  
  300.     /*
  301.      *  Current color
  302.      */
  303.     TQ3ColorRGB                        viewDiffuseColor;
  304.  
  305.     /*
  306.      *  Transforms
  307.      */
  308.     TSRRenderTransforms                transforms;
  309.     
  310.     /*
  311.      *  Clipping info in device coordinates
  312.      */
  313.     float                            clipPlanesInDC[6];
  314.         
  315.     /*
  316.      *  Current draw region and draw context
  317.      */
  318.     TQ3XDrawRegion                    drawRegion;
  319.     TQ3DrawContextObject            currentDrawContext;
  320.     
  321.     /*
  322.      *  Raster descriptor and raster base address
  323.      */
  324.     TQ3XDrawRegionDescriptor        *descriptor;
  325.     void                            *image;
  326.     
  327.     /*
  328.      *  Raster functions (clipping and non-clipping).
  329.      */
  330.     TSRRasterFunctions                rasterFunctions[2];
  331.     TSRRasterFunctions                *currentRasterFunctions;
  332.     
  333.     /*
  334.      *  Camera info
  335.      */
  336.     TQ3CameraObject                 camera;
  337.     TQ3ObjectType                    cameraType;
  338.     TQ3CameraPlacement                cameraPlacement;
  339.     
  340.     /*
  341.      *  Eye (camera) info in local and world space
  342.      */
  343.     TQ3RationalPoint4D                eyePointInLocalCoords;
  344.     TSRVector4D                        eyeVectorInLocalCoords;
  345.     TQ3RationalPoint4D                eyePointInWorldCoords;
  346.     TSRVector4D                        eyeVectorInWorldCoords;
  347.     
  348.     /*
  349.      *  Info for dealing with non-affine local-to-world transforms
  350.      */
  351.     unsigned long                    normalLocalToWorldRank;
  352.     TQ3PlaneEquation                flatWorld;
  353.     
  354.     /*
  355.      *  Counter for use in Q3XView_IdleProgress
  356.      */
  357.     unsigned long                    primitiveCount;
  358.     
  359.     /*
  360.      *    Dummy variable for configuration data. A real renderer
  361.      *    would store settings here
  362.      */
  363.      short                            dummyConfigData;
  364.      
  365. } TSRPrivate;
  366.  
  367.  
  368.  
  369. /******************************************************************************
  370.  **                                                                             **
  371.  **                            State Update Routines                             **
  372.  **                                                                             **
  373.  *****************************************************************************/
  374.  
  375. TQ3Status SR_UpdateRasterFunctions(
  376.     TSRPrivate                *srPrivate);
  377.  
  378. /*
  379.  *    Styles
  380.  */
  381. TQ3Status SR_Update_BackfacingStyle(
  382.     TQ3ViewObject             view,
  383.     TSRPrivate                *srPrivate,
  384.     TQ3BackfacingStyle        *backfacingStyle);
  385.  
  386. TQ3Status SR_Update_FillStyle(
  387.     TQ3ViewObject             view,
  388.     TSRPrivate                *srPrivate,
  389.     TQ3FillStyle            *fillStyle);
  390.  
  391. TQ3Status SR_Update_HighlightStyle(
  392.     TQ3ViewObject             view,
  393.     TSRPrivate                *srPrivate,
  394.     TQ3AttributeSet            *highlightStyle);
  395.  
  396. TQ3Status SR_Update_OrientationStyle(
  397.     TQ3ViewObject             view,
  398.     TSRPrivate                *srPrivate,
  399.     TQ3OrientationStyle        *orientationStyle);
  400.  
  401. /*
  402.  *    Attributes
  403.  */
  404. TQ3Status SR_Update_DiffuseColor(
  405.     TQ3ViewObject             view,
  406.     TSRPrivate                *srPrivate,
  407.     TQ3ColorRGB                *diffuseColor);
  408.  
  409. TQ3Status SR_Update_HighlightState(
  410.     TQ3ViewObject             view,
  411.     TSRPrivate                *srPrivate,
  412.     TQ3Boolean                *highlightSwitch);
  413.  
  414. /*
  415.  *    Matrices
  416.  */
  417. TQ3Status SR_Update_LocalToWorldMatrix(
  418.     TQ3ViewObject             view,
  419.     TSRPrivate                *srPrivate,
  420.     TQ3Matrix4x4            *localToWorld);
  421.  
  422. TQ3Status SR_Update_LocalToFrustumMatrix(
  423.     TQ3ViewObject             view,
  424.     TSRPrivate                *srPrivate,
  425.     TQ3Matrix4x4            *localToFrustum);
  426.     
  427. TQ3Status SR_Update_WorldToFrustumMatrix(
  428.     TQ3ViewObject             view,
  429.     TSRPrivate                *srPrivate,
  430.     TQ3Matrix4x4            *worldToFrustum);
  431.     
  432. /******************************************************************************
  433.  **                                                                             **
  434.  **                        Idle Progress Mechanism                                 **
  435.  **                                                                             **
  436.  *****************************************************************************/
  437.  
  438. TQ3Status SR_IdleProgress(
  439.     TQ3ViewObject    view,
  440.     TSRPrivate        *srPrivate);
  441.  
  442.  
  443. /******************************************************************************
  444.  **                                                                             **
  445.  **                        Pipeline Setup Routines                                 **
  446.  **                                                                             **
  447.  *****************************************************************************/
  448.  
  449. void SR_SetupPipelineExit(
  450.     TSRPrivate            *srPrivate);
  451.  
  452. TQ3Status SR_SetupRegionDependentTransformations(
  453.     TSRPrivate             *srPrivate);
  454.  
  455. void SR_SetupPipelineInitCamera(
  456.     TSRPrivate            *srPrivate,
  457.     TQ3CameraObject        camera);
  458.  
  459. TQ3Status SR_UpdatePipeline(
  460.     TSRPrivate            *srPrivate);
  461.  
  462.  
  463. /******************************************************************************
  464.  **                                                                             **
  465.  **                            Pipeline Routines                                 **
  466.  **                                                                             **
  467.  *****************************************************************************/
  468.  
  469. TQ3Status SR_LinePipe(
  470.     TSRPrivate             *srPrivate,
  471.     TQ3Point3D            *localVertices,
  472.     long                numVertices,
  473.     long                sizeOfLocalVertices,
  474.     TQ3ColorRGB            *color,
  475.     TQ3Vector3D            *normal,
  476.     long                mode);        
  477.  
  478. TQ3Status SR_PointPipe(
  479.     TSRPrivate             *srPrivate,
  480.     TQ3Point3D            *localVertices,
  481.     long                numVertices,
  482.     long                sizeOfLocalVertices,
  483.     TQ3ColorRGB            *color,
  484.     TQ3Vector3D            *normal,
  485.     long                mode);        
  486.  
  487.  
  488. /******************************************************************************
  489.  **                                                                             **
  490.  **                        Entry Points into Renderer                             **
  491.  **                                                                             **
  492.  *****************************************************************************/
  493.  
  494. TQ3Status SR_Geometry_Triangle(
  495.     TQ3ViewObject                 view, 
  496.     struct TSRPrivate            *srPrivate,
  497.     TQ3GeometryObject             triangle, 
  498.     const TQ3TriangleData        *triangleData);
  499.         
  500. TQ3Status SR_Geometry_Line(
  501.     TQ3ViewObject                 view, 
  502.     struct TSRPrivate            *srPrivate,
  503.     TQ3GeometryObject             line,
  504.     TQ3LineData                    *lineData);
  505.  
  506. TQ3Status SR_Geometry_Point(
  507.     TQ3ViewObject                 view, 
  508.     struct TSRPrivate            *srPrivate,
  509.     TQ3GeometryObject             point,
  510.     const TQ3PointData            *pointData);
  511.  
  512. TQ3Status SR_Geometry_Marker(
  513.     TQ3ViewObject                 view, 
  514.     struct TSRPrivate            *srPrivate,
  515.     TQ3GeometryObject             marker,
  516.     const TQ3MarkerData            *markerData);
  517.  
  518. TQ3Status SR_Geometry_PixmapMarker(
  519.     TQ3ViewObject                 view, 
  520.     struct TSRPrivate            *srPrivate,
  521.     TQ3GeometryObject             pixmapMarker,
  522.     const TQ3PixmapMarkerData    *pixmapMarkerData);
  523.  
  524. #ifdef __cplusplus
  525. }
  526. #endif    /* __cplusplus */
  527.  
  528. #endif  /*  SR_h  */
  529.